import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferFactory;
import org.jboss.netty.buffer.ChannelBufferIndexFinder;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;

public class MyChannelHandle extends SimpleChannelHandler{
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        super.messageReceived(ctx, e);
//         在Server的管道里加入了Decoder之后就可以直接使用String了
//        ChannelBuffer buf = (ChannelBuffer)e.getMessage();
//        String str = new String(buf.array());
//        System.out.println(str);

        System.out.println((String)e.getMessage());

        // 在Server的管道里加入了EnCoder以后就可以直接使用writr(string)
//        ChannelBuffer buf = ChannelBuffers.copiedBuffer("Hi Server".getBytes()) ;
//        ctx.getChannel().write(buf);

        ctx.getChannel().write("Hi Server");

        System.out.println("messageReceived\n\n");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        super.exceptionCaught(ctx, e);
        System.out.println(e.toString());
        System.out.println("exceptionCaught\n\n");
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelConnected(ctx, e);
        System.out.println("channelConnected\n\n");
    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelDisconnected(ctx, e);
        System.out.println("channelDisconnected\n\n");
    }

    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelClosed(ctx, e);
        System.out.println("channelClosed\n\n");
    }
}
